package com.uenpay.im.weight;

import android.annotation.SuppressLint;
import android.app.ActionBar.LayoutParams;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.uenpay.im.R;


/**
 * 
 *
 * @author SL
 * @Create Date: 2014-9-20 下午5:35:24
 * @copyRight http://www.qiyunxin.com
 */
public class MyDialogSingleButton extends Dialog {

	public MyDialogSingleButton(Context context) {
		super(context);
	}

	public MyDialogSingleButton(Context context, int theme) {
		super(context, theme);
	}

	public static class Builder {
		private Context context;
		private String message;
		private String cancelTextView;
		private View contentView;
		private OnClickListener cancelBtnClickListener;
		private Dialog mDialog;

		public Builder(Context context) {
			this.context = context;
		}

		public Builder setMessage(String message) {
			this.message = message;
			return this;
		}

		/**
		 * Set the Dialog message from resource
		 * 
		 * @param message
		 * @return
		 */
		public Builder setMessage(int message) {
			this.message = (String) context.getText(message);
			return this;
		}
		
		public Builder setPositiveButton(int positiveButtonText,
				OnClickListener listener) {
			this.cancelTextView = (String) context
					.getText(positiveButtonText);
			this.cancelBtnClickListener = listener;
			return this;
		}

		public Builder setContentView(View v) {
			this.contentView = v;
			return this;
		}

		public Dialog getDialog() {
			return mDialog;
		}

		@SuppressLint("NewApi")
		public MyDialogSingleButton create() {
			LayoutInflater inflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			final MyDialogSingleButton dialog = new MyDialogSingleButton(
					context, R.style.dialog);
			FrameLayout layout = (FrameLayout) inflater.inflate(
					R.layout.dialog_single_btn_layout, null);

			dialog.addContentView(layout, new LayoutParams(
					LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));

			if (cancelBtnClickListener != null) {
				((Button)layout.findViewById(R.id.cancel_btn)).setText(cancelTextView);
				((Button) layout.findViewById(R.id.cancel_btn))
						.setOnClickListener(new View.OnClickListener() {
							public void onClick(View v) {
								cancelBtnClickListener.onClick(dialog,
										DialogInterface.BUTTON_NEGATIVE);
							}
						});
			} else {
				layout.findViewById(R.id.cancel_btn).setVisibility(View.GONE);
			}
			// set the content message
			if (message != null) {
				((TextView) layout.findViewById(R.id.message)).setText(message);
			} else if (contentView != null) {
				// if no message set
				// add the contentView to the dialog body
				((LinearLayout) layout.findViewById(R.id.content_layout))
						.removeAllViews();

				((LinearLayout) layout.findViewById(R.id.content_layout))
						.addView(contentView, new LayoutParams(
								LayoutParams.MATCH_PARENT,
								LayoutParams.WRAP_CONTENT));
				((LinearLayout) layout.findViewById(R.id.content_layout))
						.setGravity(Gravity.CENTER_HORIZONTAL);

			}
			dialog.setContentView(layout);
			mDialog = dialog;
			return dialog;
		}

	}
}
